package cfca.seal.dao.main.impl;

import cfca.seal.bean.OperationLog;
import cfca.seal.common.Page;
import cfca.seal.dao.PageDao;
import cfca.seal.dao.SmartDao;
import cfca.seal.dao.main.IOperationLogDao;
import cfca.seal.exception.CodeException;
import cfca.seal.system.Env;
import cfca.seal.util.StringUtil;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class OperationLogDao
  implements IOperationLogDao
{
  private static String tableName = "操作日志";
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  @Resource
  private SmartDao smartDao;

  @Resource
  private PageDao pageDao;

  @Override
  public boolean insert(OperationLog operationLog) { try { String sql = "INSERT INTO OPERATION_LOG (ID, CREATE_TIME, APPLICATION_NAME, ORGANIZATION_ID, TYPE, OBJECT_ID, OPERATOR, DESCRIPTION, FILE_NAME, RESULT) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

      Object[] args = { StringUtil.generateID(), new Date(), operationLog.getApplicationName(), operationLog.getOrganizationId(), operationLog.getType(), operationLog.getObjectId(), operationLog.getOperator(), operationLog.getDescription(), operationLog.getFileName(), Integer.valueOf(operationLog.getResult()) };

      log.debug("insert operation_log sql: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
    }return false; }

  public Page<OperationLog> getPage(OperationLog operationLog, String pageno, String operator)
    throws CodeException
  {
    try
    {
      List conditionList = new ArrayList();
      List args = new ArrayList();

      if (StringUtil.isNotEmpty(operator)) {
        conditionList.add("OPERATOR = ?");
        args.add(operator);
      }

      if (StringUtil.isNotEmpty(operationLog.getType())) {
        conditionList.add("TYPE like ?");
        args.add("%" + operationLog.getType() + "%");
      }

      if (operationLog.getResult() != 0) {
        conditionList.add("RESULT = ?");
        args.add(Integer.valueOf(operationLog.getResult()));
      }

      if (StringUtil.isNotEmpty(operationLog.getOperator())) {
        conditionList.add("OPERATOR = ?");
        args.add(operationLog.getOperator());
      }

      if (StringUtil.isNotEmpty(operationLog.getObjectId())) {
        conditionList.add("OBJECT_ID = ?");
        args.add(operationLog.getObjectId());
      }

      if (StringUtil.isNotEmpty(operationLog.getOrganizationId())) {
        conditionList.add("ORGANIZATION_ID = ?");
        args.add(operationLog.getOrganizationId());
      }

      if (StringUtil.isNotEmpty(operationLog.getStartTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          conditionList.add("DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') >= DATETIME(?)");
          args.add(operationLog.getStartTime());
        } else if ("mysql".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME >= date_format(?,'%y-%m-%d %H:%i:%s')");
          args.add(operationLog.getStartTime());
        } else if ("oracle".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME >= to_date(?,'yyyy-mm-dd HH24:MI:SS')");
          args.add(operationLog.getStartTime());
        } else if ("db2".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME >= to_date(?,'yyyy-mm-dd hh24:mi:ss')");
          args.add(operationLog.getStartTime());
        }
      }

      if (StringUtil.isNotEmpty(operationLog.getEndTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          conditionList.add("DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') <= DATETIME(?)");
          args.add(operationLog.getEndTime());
        } else if ("mysql".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME <= date_format(?,'%y-%m-%d %H:%i:%s')");
          args.add(operationLog.getEndTime());
        } else if ("oracle".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME <= to_date(?,'yyyy-mm-dd HH24:MI:SS')");
          args.add(operationLog.getEndTime());
        } else if ("db2".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME <= to_date(?,'yyyy-mm-dd hh24:mi:ss')");
          args.add(operationLog.getEndTime());
        }
      }

      return this.pageDao.dispage(OperationLog.class, "OPERATION_LOG", conditionList, args.toArray(), pageno, 20);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public List<OperationLog> getLogForArchive(OperationLog operationLog, String operator) throws Exception
  {
    try {
      List args = new ArrayList();
      String sql = "SELECT * FROM OPERATION_LOG WHERE 1=1 ";

      if (StringUtil.isNotEmpty(operator)) {
        sql = sql + " ADN OPERATOR = ?";
        args.add(operator);
      }

      if (StringUtil.isNotEmpty(operationLog.getStartTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          sql = sql + " AND DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') >= DATETIME(?)";
          args.add(operationLog.getStartTime());
        } else if ("mysql".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME >= date_format(?,'%y-%m-%d %H:%i:%s')";
          args.add(operationLog.getStartTime());
        } else if ("oracle".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME >= to_date(?,'yyyy-mm-dd HH24:MI:SS')";
          args.add(operationLog.getStartTime());
        } else if ("db2".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME >= to_date(?,'yyyy-mm-dd hh24:mi:ss')";
          args.add(operationLog.getStartTime());
        }
      }

      if (StringUtil.isNotEmpty(operationLog.getEndTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          sql = sql + " AND DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') <= DATETIME(?)";
          args.add(operationLog.getEndTime());
        } else if ("mysql".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME <= date_format(?,'%y-%m-%d %H:%i:%s')";
          args.add(operationLog.getEndTime());
        } else if ("oracle".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME <= to_date(?,'yyyy-mm-dd HH24:MI:SS')";
          args.add(operationLog.getEndTime());
        } else if ("db2".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME <= to_date(?,'yyyy-mm-dd hh24:mi:ss')";
          args.add(operationLog.getEndTime());
        }
      }

      return this.smartDao.getList(OperationLog.class, sql, args.toArray());
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public void delete(OperationLog operationLog, String operator) throws Exception
  {
    try {
      List args = new ArrayList();
      String sql = "DELETE FROM OPERATION_LOG WHERE 1=1 ";

      if (StringUtil.isNotEmpty(operator)) {
        sql = sql + " ADN OPERATOR = ?";
        args.add(operator);
      }

      if (StringUtil.isNotEmpty(operationLog.getStartTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          sql = sql + " AND DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') >= DATETIME(?)";
          args.add(operationLog.getStartTime());
        } else if ("mysql".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME >= date_format(?,'%y-%m-%d %H:%i:%s')";
          args.add(operationLog.getStartTime());
        } else if ("oracle".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME >= to_date(?,'yyyy-mm-dd HH24:MI:SS')";
          args.add(operationLog.getStartTime());
        } else if ("db2".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME >= to_date(?,'yyyy-mm-dd hh24:mi:ss')";
          args.add(operationLog.getStartTime());
        }
      }

      if (StringUtil.isNotEmpty(operationLog.getEndTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          sql = sql + " AND DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') <= DATETIME(?)";
          args.add(operationLog.getEndTime());
        } else if ("mysql".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME <= date_format(?,'%y-%m-%d %H:%i:%s')";
          args.add(operationLog.getEndTime());
        } else if ("oracle".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME <= to_date(?,'yyyy-mm-dd HH24:MI:SS')";
          args.add(operationLog.getEndTime());
        } else if ("db2".equals(Env.databaseType)) {
          sql = sql + " AND CREATE_TIME <= to_date(?,'yyyy-mm-dd hh24:mi:ss')";
          args.add(operationLog.getEndTime());
        }
      }

      int count = this.smartDao.update(sql, args.toArray());
      System.out.println("delete no: " + count);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1004", tableName + "表删除失败", e);
    }
  }
}